﻿using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace library
{
    public partial class booking : Form
    {
        public MySqlConnection conn = new DBclass().getConn();
        public booking()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            conn.Open();
            string sql = "select * from booking_info where uID=@uid";
            MySqlCommand cmd = new MySqlCommand(sql, conn);
            cmd.Parameters.Add("@uid", MySqlDbType.VarChar, 6).Value = User.uID;
            MySqlDataAdapter adapter = new MySqlDataAdapter(cmd);
            DataSet dataSet = new DataSet();
            adapter.Fill(dataSet);
            DataTable dataTable = dataSet.Tables[0];

            int count = dataTable.Rows.Count;
            string[] bnames = new string[count];

            for (int i = 0; i < count; i++)
            {
                string sql2 = "select bname from book1_info where ISBN=@isbn";
                MySqlCommand cmd2 = new MySqlCommand(sql2, conn);
                cmd2.Parameters.Add("@isbn", MySqlDbType.VarChar, 17).Value = dataTable.Rows[i]["ISBN"].ToString(); ;
                MySqlDataAdapter adapter2 = new MySqlDataAdapter(cmd2);
                DataSet dataSet2 = new DataSet();
                adapter2.Fill(dataSet2);
                DataTable dataTable2 = dataSet2.Tables[0];
                bnames[i] = dataTable2.Rows[0]["bname"].ToString();

            }

            dataTable.Columns.Add("bname");
            for (int i = 0; i < count; i++)
            {

                dataTable.Rows[i]["bname"] = bnames[i];
            }

            dataGridView1.AutoGenerateColumns = false;
            dataGridView1.DataSource = dataTable;
            this.dataGridView1.Columns["id"].DataPropertyName = dataTable.Columns["id"].ToString();
            this.dataGridView1.Columns["ISBN"].DataPropertyName = dataTable.Columns["ISBN"].ToString();
            this.dataGridView1.Columns["书名"].DataPropertyName = dataTable.Columns["bname"].ToString();
            this.dataGridView1.Columns["预定时间"].DataPropertyName = dataTable.Columns["booking_time"].ToString();
            this.dataGridView1.Columns["预定状态"].DataPropertyName = dataTable.Columns["booking_status"].ToString();

            conn.Close();

        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0)
            {
                DataGridViewColumn column = dataGridView1.Columns[e.ColumnIndex];
                if (column is DataGridViewButtonColumn)
                {
                    string status = dataGridView1.Rows[e.RowIndex].Cells[4].Value.ToString();
                    if(e.ColumnIndex==5)
                    {
                        if (status == "待取走" || status == "已取走")
                        {
                            MessageBox.Show("这本书已经预约成功不能取消了");
                        }
                        else if (status == "已取消")
                        {
                            MessageBox.Show("这本书已经取消预定了，请不要重复点击");
                        }
                        else if (status == "已预约")
                        {
                            int id = Convert.ToInt32(dataGridView1.Rows[e.RowIndex].Cells[0].Value);
                            conn.Open();
                            string sql = "update booking_info set booking_status='已取消' where id=@id";
                            MySqlCommand cmd = new MySqlCommand(sql, conn);
                            cmd.Parameters.Add("@id", MySqlDbType.Int32, 11).Value = id;
                            int result = cmd.ExecuteNonQuery();
                            if (result > 0)
                            {
                                MessageBox.Show("取消预定成功");
                            }
                        }

                        conn.Close();
                    }
                    if(e.ColumnIndex==6)
                    {
                        if(status=="已预约")
                        {
                            string ISBN = dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString();
                            showdetail showDetail = new showdetail(ISBN);
                            showDetail.Show();
                        }
                        else
                        {
                            MessageBox.Show("只能查看预约排队中的图书");
                        }
                    }
                    
                }
            }
        }
    }
}
